<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .img{
            width: 350px;
            height: 350px;
            background-image: url(../imgs/1.jpg);
            float: left;
            position: relative;
        }
        .enlarge{
            width: 200px;
            height: 200px;
            background-image: url(../imgs/1.big.jpg);
            display: flex;
            float: left;
            background-repeat: no-repeat;
        }
        .img .mask{
            position: absolute;
            top: 0;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: orange;
            opacity: 0.5;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="img">
            <div class="mask"></div>
        </div>
        <div class="enlarge"></div>
    </div>
    <script>
        //获取图片
        let imgObj = document.querySelector('.box .img')
        imgObj.onmousemove=function(evt){
            //获取事件对象
         let e = evt||window.event
        //获取盒模型和遮罩
         let boxObj = document.querySelector('.box')
         let maskObj = document.querySelector('.mask')
        //设置
        let left = e.pageX-boxObj.offsetLeft-maskObj.offsetWidth/2
        let top = e.pageY-boxObj.offsetTop-maskObj.offsetHeight/2
        maskObj.style.left=left+'px'
        maskObj.style.top=top+'px'
        //设置遮罩图活动范围
        if(left<0) maskObj.style.left=0+'px'
        if(top<0)   maskObj.style.top=0+'px'
        if(left>250) maskObj.style.left=250+'px'
        if(top>250)   maskObj.style.top=250+'px'
        //enlarge随着遮罩图动而动
        let enlargeObj=document.querySelector('.enlarge')
        // enlargeObj.style.left=-2*left+'px'
        // enlargeObj.style.top=-2*top+'px'
        //用background-position调试位置
        enlargeObj.style.backgroundPosition=`${-2*left}px  ${-2*top}px  `
        }
    </script>
</body>
</html>